package com.zhida.jiyangcenter.splash

import android.content.Context
import android.content.Intent
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.resColor
import chooongg.base.utils.setStatusBarLightMode
import chooongg.base.utils.startActivity
import chooongg.base.widget.backlayout.ParallaxBackEnable
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.WebNoTokenActivity
import com.zhida.jiyangcenter.account.LoginPassActivity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.mian.MainActivity
import com.zhida.jiyangcenter.utils.SP
import java.io.File

@TitleBar(false)
@ParallaxBackEnable(false)
@ContentLayout(R.layout.activity_splash)
class SplashActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        setStatusBarLightMode(true)
        getPermission()
    }

    fun getPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            AndPermission.with(this)
                .runtime()
                .permission(
                    Permission.ACCESS_FINE_LOCATION,
                    Permission.READ_EXTERNAL_STORAGE,
                    Permission.WRITE_EXTERNAL_STORAGE,
                    Permission.CAMERA
                )
                .onGranted {
                    if (it.contains(Permission.WRITE_EXTERNAL_STORAGE) && it.contains(Permission.READ_EXTERNAL_STORAGE)) {
                        if (SP.isLogin.get()) {
//                            if (!SP.ISYinsi.get()) {
//                                startActivity<WebNoTokenActivity> {
//                                    putExtra(WebNoTokenActivity.URL, "http://beauty.hebzd.com/static/xieyi/privacy.html")
//                                    putExtra(WebNoTokenActivity.TITLE, "隐私协议")
//                                }
//                            } else {
                                startActivity<MainActivity> { }
//                            }
                        }else{
                            startActivity<LoginPassActivity> { }
                        }
                        finish()
                    } else {
                        getPermission()
                    }
                }.onDenied {
                    if (AndPermission.hasAlwaysDeniedPermission(context, it)) {
                        var name = ""
                        it.forEach { it1 ->
                            name += when (it1) {
                                Permission.ACCESS_FINE_LOCATION -> "定位权限\r\n"
                                Permission.READ_EXTERNAL_STORAGE -> "读取权限\r\n"
                                Permission.WRITE_EXTERNAL_STORAGE -> "写入权限\r\n"
                                Permission.CAMERA -> "相机权限\r\n"
                                else -> ""
                            }
                        }

                        val dialog = NormalDialog(context)
                        dialog.content("请开启您的：$name")
                            .style(NormalDialog.STYLE_TWO)//
                            .title("请开启您的权限！")
                            .btnNum(1)
                            .btnTextColor(resColor(R.color.colorAccent))
                            .btnText("马上开启")
                            .titleTextSize(23f)
                            .showAnim(BounceTopEnter())
                            .dismissAnim(SlideBottomExit())
                            .show()
                        dialog.setCanceledOnTouchOutside(false)
                        dialog.setCancelable(false)
                        dialog.setOnDismissListener {
                            AndPermission.with(this).runtime().setting().start(23)
                        }
                        dialog.setOnBtnClickL(OnBtnClickL {
                            dialog.dismiss()
                        })
                    }
                }.start()
        } else {
            AndPermission.with(this)
                .runtime()
                .permission(
                    Permission.ACCESS_FINE_LOCATION,
                    Permission.CAMERA
                )
                .onGranted {
                    if (SP.isLogin.get()) {
//                        if (!SP.ISYinsi.get()) {
//                            startActivity<WebNoTokenActivity> {
//                                putExtra(WebNoTokenActivity.URL, "http://beauty.hebzd.com/static/xieyi/privacy.html")
//                                putExtra(WebNoTokenActivity.TITLE, "隐私协议")
//                            }
//                        } else {
                            startActivity<MainActivity> { }
//                        }
                    }else{
                        startActivity<LoginPassActivity> { }
                    }
                    finish()
                }.onDenied {
                    if (AndPermission.hasAlwaysDeniedPermission(context, it)) {
                        var name = ""
                        it.forEach { it1 ->
                            name += when (it1) {
                                Permission.ACCESS_FINE_LOCATION -> "定位权限\r\n"
                                Permission.CAMERA -> "相机权限\r\n"
                                else -> ""
                            }
                        }

                        val dialog = NormalDialog(context)
                        dialog.content("请开启您的：$name")
                            .style(NormalDialog.STYLE_TWO)//
                            .title("请开启您的权限！")
                            .btnNum(1)
                            .btnTextColor(resColor(R.color.colorAccent))
                            .btnText("马上开启")
                            .titleTextSize(23f)
                            .showAnim(BounceTopEnter())
                            .dismissAnim(SlideBottomExit())
                            .show()
                        dialog.setCanceledOnTouchOutside(false)
                        dialog.setCancelable(false)
                        dialog.setOnDismissListener {
                            AndPermission.with(this).runtime().setting().start(23)
                        }
                        dialog.setOnBtnClickL(OnBtnClickL {
                            dialog.dismiss()
                        })
                    }
                }.start()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            23 -> if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && !AndPermission.hasPermissions(
                    this, Permission.READ_EXTERNAL_STORAGE,
                    Permission.WRITE_EXTERNAL_STORAGE
                )) || !AndPermission.hasPermissions(
                    this, Permission.ACCESS_FINE_LOCATION,
                    Permission.CAMERA
                )
            ) {
                var name = ""
                if (!AndPermission.hasPermissions(this, Permission.ACCESS_FINE_LOCATION)) {
                    name += "定位权限\r\n"
                }
                if (!AndPermission.hasPermissions(this, Permission.CAMERA)) {
                    name += "相机权限\r\n"
                }
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                    if (!AndPermission.hasPermissions(this, Permission.READ_EXTERNAL_STORAGE)) {
                        name += "读取权限\r\n"
                    }
                    if (!AndPermission.hasPermissions(this, Permission.WRITE_EXTERNAL_STORAGE)) {
                        name += "写入权限\r\n"
                    }
                }

                val dialog = NormalDialog(context)
                dialog.content("请开启您的：$name")
                    .style(NormalDialog.STYLE_TWO)//
                    .title("请开启您的权限！")
                    .btnNum(1)
                    .btnTextColor(resColor(R.color.colorAccent))
                    .btnText("马上开启")
                    .titleTextSize(23f)
                    .showAnim(BounceTopEnter())
                    .dismissAnim(SlideBottomExit())
                    .show()
                dialog.setCanceledOnTouchOutside(false)
                dialog.setCancelable(false)
                dialog.setOnDismissListener {
                    AndPermission.with(this).runtime().setting().start(23)
                }
                dialog.setOnBtnClickL(OnBtnClickL {
                    dialog.dismiss()
                })
            } else {
                if (SP.isLogin.get()) {
//                    if (!SP.ISYinsi.get()) {
//                        startActivity<WebNoTokenActivity> {
//                            putExtra(WebNoTokenActivity.URL, "http://beauty.hebzd.com/static/xieyi/privacy.html")
//                            putExtra(WebNoTokenActivity.TITLE, "隐私协议")
//                        }
//                    } else {
                        startActivity<MainActivity> { }
//                    }
                }else{
                    startActivity<LoginPassActivity> { }
                }
                finish()
//                    if (!SP.IsClearPhotos.get()) {
//                        SP.IsClearPhotos.put(true)
//                        val file = File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.path, "zhida")
//                        file.deleteDir()
//                        withContext(Dispatchers.Main) {
//                            updateFileFromDatabase(context, file)
//                        }
//                    }
//                    delay(2000)
            }
        }
    }


    fun updateFileFromDatabase(context: Context, file: File) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val paths =
                arrayOf(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString())
            MediaScannerConnection.scanFile(context, paths, null, null)
            MediaScannerConnection.scanFile(
                context,
                arrayOf(file.absolutePath),
                null
            ) { path, uri -> }
        } else {
            context.sendBroadcast(
                Intent(
                    Intent.ACTION_MEDIA_MOUNTED, Uri.parse(
                        "file://" + context.getExternalFilesDir(
                            Environment.DIRECTORY_PICTURES
                        )
                    )
                )
            )
        }
    }
}